/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iduxs.collect.utils;

import com.iduxs.collect.frame.base.BaseBo;
import com.iduxs.log.MyLog;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 *
 * @author Administrator
 */
public class Utility {

    public static String getUserDir() {
        return System.getProperty("user.dir");
    }

    public static void writeXMLFile(String outputFilename, BaseBo bo) throws Exception {
        MyLog.getServerlog().info(bo);
        DocumentFactory factory = DocumentFactory.getInstance();
        org.dom4j.Document doc = factory.createDocument();
        doc.setXMLEncoding("UTF-8");
        Element root = doc.addElement("root");
        for (Method method : bo.getClass().getMethods()) {
            String methodName = method.getName();
            if (methodName.startsWith("get")) {
                String name = methodName.substring(3);
                Method getMethod = bo.getClass().getMethod(methodName);
                String value=null;
                if(getMethod.invoke(bo)!=null){
                 value= getMethod.invoke(bo).toString();
                }
                Element element = root.addElement(name);
                element.addCDATA(value);
            } else if (Boolean.class.equals(method.getReturnType()) && methodName.startsWith("is")) {
                String name = methodName.substring(2);
                Method getMethod = bo.getClass().getMethod(methodName);
                String value=null;
                if(getMethod.invoke(bo)!=null){
                 value= getMethod.invoke(bo).toString();
                }
                Element element = root.addElement(name);
                element.addCDATA(value);
            }
        }
        XMLWriter writer = new XMLWriter();
        File file = new File(getUserDir() + "/config/" + outputFilename);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        FileOutputStream fout = new FileOutputStream(file);
        BufferedOutputStream bout = new BufferedOutputStream(fout);
        writer.setOutputStream(bout);
        writer.write(doc);
        fout.close();
    }
    
    public static BaseBo readXMLFile(String inputFilename, BaseBo bo) throws Exception {
        File file = new File(getUserDir() + "/config/" + inputFilename);
        if (!file.exists()) {
            throw new Exception("file not exist : "+getUserDir() + "/config/" + inputFilename);
        }
            Map<String,Method> methodMap = new HashMap<String, Method>();
        for (Method method:bo.getClass().getMethods()) {
            methodMap.put(method.getName(), method);
        }
        SAXReader reader = new SAXReader(); 
        org.dom4j.Document  document = reader.read(file);
        Element root = document.getRootElement();
        for (Iterator it = root.elementIterator();it.hasNext();) {
            Element element = (Element) it.next();
            String methodName = "set" + element.getName();
            if (methodMap.containsKey(methodName)) {
                Method setMethod = methodMap.get(methodName);
                Object obj = element.getStringValue();
                if(obj!=null&&((String)obj).length()>0){
                if (setMethod.getParameterTypes()[0].equals(String.class)) {
                    obj = ((String)obj).trim();
                } else if (setMethod.getParameterTypes()[0].equals(Boolean.class)) {
                    obj = Boolean.valueOf((String)obj);
                } else if (setMethod.getParameterTypes()[0].equals(Integer.class)) {
                    obj = Integer.valueOf((String)obj);
                } else if (setMethod.getParameterTypes()[0].equals(Double.class)) {
                    obj = Double.valueOf((String)obj);
                } else if (setMethod.getParameterTypes()[0].equals(Long.class)) {
                    obj = Long.valueOf((String)obj);
                } else if (setMethod.getParameterTypes()[0].equals(Float.class)) {
                    obj = Float.valueOf((String)obj);
                } else if (setMethod.getParameterTypes()[0].equals(Short.class)) {
                    obj = Short.valueOf((String)obj);
                } else if (setMethod.getParameterTypes()[0].equals(Byte.class)) {
                    obj = Byte.valueOf((String)obj);
                } else if (setMethod.getParameterTypes()[0].equals(Character.class)) {
                    obj = Character.valueOf(((String)obj).charAt(0));
                }
                setMethod.invoke(bo, new Object[]{obj});

                }
            }
        }
        return bo;
    }
    
}
